// pages/mechanism/index.js
var request = require("../../../utils/request.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex:0,
    contentHeight:0,
    organization:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var data = wx.getStorageSync("organizationData");
    

    for (var i = 0; i < data.Courses.length; i++) {
      var pic = data.Courses[i]['Pic'].split(',')

      for (var j in pic) {
        pic[j] = "http://qbonbon.qclub-online.com/_Document/Image/" + pic[j]

      }

      data.Courses[i]['Pic'] = pic[0];
     
    }
    console.log(data);
    if(data != undefined){
      this.setData({
        "organization":data
      })
    }
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    that.init();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  setTab(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(id);
    this.setData({
      'currentIndex': id
    });
  },

  swiperChange(e) {
    var that = this;
    var id = e.detail.current;
    this.setData({
      'currentIndex': id
    });
  },
  
  init:function(){
    var that = this;
    const query = wx.createSelectorQuery()
    query.select('#courseHeight').boundingClientRect()

    query.exec(function (res) {
      console.log(res);
      
      that.setData({
        contentHeight: res[0].height
      })
    })

  },

  follow: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    request.request("api/Organization/ChangeFocusState", { OrganizationId: id, CustomerId: app.globalData.userInfo['ID'] }, 'post', 1).then(function (res) {
      if (res.data.status == 1) {
        wx.showToast({
          title: '操作成功',
        });

        var focus = that.data.organization.HasFocus == true ? false : true;

        that.setData({
          "organization.HasFocus": focus
        });

        var page = getCurrentPages();
        page[page.length - 2].onLoad();

      }
    });
    return false;
  },

  //跳转普通课程
  detail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/course/detail?id=' + id,
    })
  }
})